﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace VIP.Manager {

    /// <summary>
    /// 音频类型
    /// </summary>
    public enum AudioType
    {
        BGM,
        Standard,
        Speak,
        Button
    }

    /// <summary>
    /// 音频码
    /// </summary>
    public enum AudioCode
    {
        UIButton,
        Speak
    }

    public class AudioManager : VIPMonoSingleton<AudioManager>
    {

        private Dictionary<AudioCode, string> pathDic;
        /// <summary>
        /// 给音频的ID分类
        /// </summary>
        private Dictionary<AudioType, string> typeDic;

        /// <summary>
        /// 初始化
        /// </summary>
        private void Start()
        {
            pathDic = new Dictionary<AudioCode, string>();
            typeDic = new Dictionary<AudioType, string>();
            Init();
        }

        /// <summary>
        /// 播放声音
        /// </summary>
        /// <param name="audioType">声音类型（背景、按钮、说话、标准等等）</param>
        /// <param name="audioCode">声音码（警告声、按钮声等等）</param>
        public void Play(AudioType audioType, AudioCode audioCode)
        {
            string path;
            if (pathDic.TryGetValue(audioCode, out path))
            {
                //根据路径转换成音频片段
                AudioClip clip = VIPResourceUtil.GetAudio(path);
                string idStr;
                //根据音频类型获取音频ID
                bool bo = typeDic.TryGetValue(audioType, out idStr);
                //如果bo为True证明该类型对应的音频正在播放，否则对应类型无播放的音频
                if (bo)
                {
                    //根据ID停止音频
                    typeDic.Remove(audioType);
                    VIPAudio.Instance.Stop(idStr);
                    //播放音频，并返回对应音频的ID
                    string id = VIPAudio.Instance.Play(clip, () => {
                        //音频播放完成移除字典对应的键值对
                        typeDic.Remove(audioType);
                    }, true);
                    //将音频ID根据对应类型添加到字典
                    typeDic.Add(audioType, id);
                }
                else
                {
                    //播放音频，并返回对应音频的ID
                    string id = VIPAudio.Instance.Play(clip, () => {
                        //音频播放完成移除字典对应的键值对
                        typeDic.Remove(audioType);
                    }, true);
                    //将音频ID根据对应类型添加到字典
                    typeDic.Add(audioType, id);
                }
            }
            else
            {
                print("未找到路径");
            }
        }

        /// <summary>
        /// 注册音频
        /// </summary>
        private void Init()
        {
            //按钮
            pathDic.Add(AudioCode.UIButton, "Audios/Button/UIButton");
            pathDic.Add(AudioCode.Speak, "Audios/Speak/Speak");
        }

    }

}

